import requests
import base64

    #---------------------------联想存储--------------------------------
def lenovo_cunchu():
    # 替换为您的存储的登录信息
    fssc_username = "xxxxxxxxxxxxxx"
    fssc_password = "xxxxxxxxxxxxxxxxxxxx"
    # fssc_login_url = "https://10.106.133.100/security/login"
    # 将用户名和密码编码为Base64字符串
    fssc_auth = base64.b64encode(f"{fssc_username}:{fssc_password}".encode()).decode()


    # 构建认证请求
    fssc_headers = {
        "Content-Type": "application/json",
        "Accept": "application/hal+json",
        "Authorization": f"Basic {fssc_auth}"
    }

    #=======================================财务共享存储获取数据开始=================================================
    #获取财务共享nas 卷 fssc_nas数据
    fssc_storage_vloumes_url = "https://x.x.x.x/api/storage/volumes/6800e4a9-241f-11ed-83c8-d039ea294330"
    requests.packages.urllib3.disable_warnings()
    storage_vloumes_response = requests.get(fssc_storage_vloumes_url, headers=fssc_headers, verify=False)
    if storage_vloumes_response.status_code == 200:
        print("Login success")
        # print(dir(storage_vloumes_response))
        storage_vloumes_json = storage_vloumes_response.json()
        #print(storage_vloumes_response.text)
        storage_vloumes_json_used_B = storage_vloumes_json['space']['used']
        storage_vloumes_json_used_T = round(storage_vloumes_json_used_B / 1024 / 1024 / 1024 / 1024,2)
        #print(f"财务共享nas存储已用空间：{storage_vloumes_json_used_T}")

        storage_vloumes_json_available_B = storage_vloumes_json['space']['available']
        storage_vloumes_json_available_T = round(storage_vloumes_json_available_B / 1024 / 1024 / 1024 / 1024, 2)
        #print(f"财务共享nas存储可用空间：{storage_vloumes_json_available_T}")

        storage_vloumes_json_size_B = storage_vloumes_json['space']['size']
        storage_vloumes_json_size_T = round(storage_vloumes_json_size_B / 1024 / 1024 / 1024 / 1024, 2)
        #print(f"财务共享nas存储总空间：{storage_vloumes_json_size_T}")
        caiwunas_usage =  round(float(storage_vloumes_json_used_T / storage_vloumes_json_size_T * 100),3)
        #print(f"财务共享nas存储使用率： {caiwunas_usage}")
        if caiwunas_usage > 80:
            caiwunas_usage = f'<span style="color:red;">{caiwunas_usage}</span>'
        else:
            caiwunas_usage = f'<span style="color:green;">{caiwunas_usage}</span>'
    else:
        print("Login failed!")
    #财务共享存储日志信息
    fssc_log_url = "https://x.x.x.x/api/support/ems/events?return_timeout=120&fields=time,node,message,,log_message,index&order_by=time desc&message.severity=emergency|alert|error&time=>=24h"
    requests.packages.urllib3.disable_warnings()
    fssc_log_url_response = requests.get(fssc_log_url, headers=fssc_headers, verify=False)
    if fssc_log_url_response.status_code == 200:
        print("Login success")
       # print(fssc_log_url_response.text)
        fssc_log_url_response_json = fssc_log_url_response.json()
        fssc_log_records = fssc_log_url_response_json["records"]
       # print("+++++++++++++++++++++++")
        #print(len(fssc_log_records))
        if len(fssc_log_records) > 0:
            fssc_log_message_shuzu = []
            fssc_log_message = ""
            filtered_records = [record for record in fssc_log_records if record["log_message"].find("sis.changelog.full") == -1]
            if len(filtered_records) > 0:
                for fssc_records in filtered_records:
                    fssc_log_message_time = fssc_records["time"]
                    fssc_message_severity = fssc_records["message"]["severity"]
                    fssc_log_message_i = fssc_records["log_message"]
                    fssc_log_message_shuzu.append(f"告警时间：{fssc_log_message_time}</br> 日志级别：{fssc_message_severity}, </br> 日志内容:{fssc_log_message_i}")
                for i in fssc_log_message_shuzu:
                    fssc_log_message += str(i) + "</br>"
            else:
                fssc_log_message = "财务nas存储设备只有sis.changelog.full日志产生,请及时关注"
        else:
            fssc_log_message = "财务nas存储设备无告警日志产生"

        print(fssc_log_message)
    #财务共享存储设备总容量
    fssc_storage_url = "https://x.x.x.x/api/storage/cluster"
    requests.packages.urllib3.disable_warnings()
    fssc_storage_response = requests.get(fssc_storage_url, headers=fssc_headers, verify=False)
    if fssc_storage_response.status_code == 200:
        print("Login success")
        fssc_storage_response_json = fssc_storage_response.json()
        fssc_storage_full_size_B = fssc_storage_response_json["block_storage"]["size"]
        fssc_storage_full_size_T = round(fssc_storage_full_size_B / 1024 / 1024 / 1024 / 1024, 2)
        print(f"存储总空间: {fssc_storage_full_size_T}")
        fssc_storage_full_used_B = fssc_storage_response_json["block_storage"]['used']
        fssc_storage_full_used_T = round(fssc_storage_full_used_B / 1024 / 1024 / 1024 / 1024, 2)
        print(fssc_storage_full_used_T)

        fssc_storage_usage = round(float(fssc_storage_full_used_T / fssc_storage_full_size_T) * 100, 3)
        if fssc_storage_usage > 80:
            fssc_storage_usage = f'<span style="color:red;">{fssc_storage_usage}</span>'
        else:
            fssc_storage_usage = f'<span style="color:green;">{fssc_storage_usage}</span>'
        print(fssc_storage_usage)
    #=======================================财务共享存储获取数据结束=================================================
    print("=======================================财务共享存储获取数据结束=================================================")
    #=======================================NC存储获取数据开始=================================================
    nc_username = "xxxxxxxxx"
    nc_password = "xxxxxxxxxxxxxxxx"
    nc_auth = base64.b64encode(f"{nc_username}:{nc_password}".encode()).decode()
    nc_headers = {
        "Content-Type": "application/json",
        "Accept": "application/hal+json",
        "Authorization": f"Basic {nc_auth}"
    }
    #获取NC卷容量数据
    nc_storage_volume_url = "https://x.x.x.x/api/storage/volumes?return_timeout=120&fields=name,state,space&is_object_store=false&order_by=name&state=!null"
    requests.packages.urllib3.disable_warnings()
    nc_storage_volume_url_response = requests.get(nc_storage_volume_url, headers=nc_headers, verify=False)
    if nc_storage_volume_url_response.status_code == 200:
        print("NC存储登录成功")
        #print(nc_storage_volume_url_response.text)
        nc_storage_volume_url_response_json = nc_storage_volume_url_response.json()
        nc_storage_volume_url_response_json_records = nc_storage_volume_url_response_json["records"]

        nc_storage_volume_nas_muyuan_size = round(nc_storage_volume_url_response_json_records[0]['space']["size"] / 1024 / 1024 / 1024 ,2)
        nc_storage_volume_nas_muyuan_used = round(nc_storage_volume_url_response_json_records[0]['space']["used"] / 1024 / 1024 / 1024 ,2)
        nc_storage_volume_nas_muyuan_useage = round(nc_storage_volume_url_response_json_records[0]['space']["used"]*100 / nc_storage_volume_url_response_json_records[0]['space']["size"],2)
        if nc_storage_volume_nas_muyuan_useage > 80:
            nc_storage_volume_nas_muyuan_useage = f'<span style="color:red;">{nc_storage_volume_nas_muyuan_useage}</span>'
        else:
            nc_storage_volume_nas_muyuan_useage = f'<span style="color:green;">{nc_storage_volume_nas_muyuan_useage}</span>'

        nc_storage_volume_ncdb_asm_size = round(nc_storage_volume_url_response_json_records[1]['space']["size"] / 1024 / 1024 / 1024 /1024, 2)
        nc_storage_volume_ncdb_asm_used = round(nc_storage_volume_url_response_json_records[1]['space']["used"] / 1024 / 1024 / 1024 /1024, 2)
        nc_storage_volume_ncdb_asm_useage = round(nc_storage_volume_url_response_json_records[1]['space']["used"] * 100 / nc_storage_volume_url_response_json_records[1]['space']["size"], 2)
        if nc_storage_volume_ncdb_asm_useage > 80:
            nc_storage_volume_ncdb_asm_useage = f'<span style="color:red;">{nc_storage_volume_ncdb_asm_useage}</span>'
        else:
            nc_storage_volume_ncdb_asm_useage = f'<span style="color:green;">{nc_storage_volume_ncdb_asm_useage}</span>'


        nc_storage_volume_ncdb_asm2_size = round(nc_storage_volume_url_response_json_records[2]['space']["size"] / 1024 / 1024 / 1024 /1024, 2)
        nc_storage_volume_ncdb_asm2_used = round(nc_storage_volume_url_response_json_records[2]['space']["used"] / 1024 / 1024 / 1024 /1024, 2)
        nc_storage_volume_ncdb_asm2_useage = round(nc_storage_volume_url_response_json_records[2]['space']["used"] * 100 / nc_storage_volume_url_response_json_records[2]['space']["size"], 2)
        if nc_storage_volume_ncdb_asm2_useage > 80:
            nc_storage_volume_ncdb_asm2_useage = f'<span style="color:red;">{nc_storage_volume_ncdb_asm2_useage}</span>'
        else:
            nc_storage_volume_ncdb_asm2_useage = f'<span style="color:green;">{nc_storage_volume_ncdb_asm2_useage}</span>'


        nc_storage_volume_ncdb_back_size = round(nc_storage_volume_url_response_json_records[3]['space']["size"] / 1024 / 1024 / 1024 /1024, 2)
        nc_storage_volume_ncdb_back_used = round(nc_storage_volume_url_response_json_records[3]['space']["used"] / 1024 / 1024 / 1024 /1024, 2)
        nc_storage_volume_ncdb_back_useage = round(nc_storage_volume_url_response_json_records[3]['space']["used"] * 100 / nc_storage_volume_url_response_json_records[3]['space']["size"], 2)
        if nc_storage_volume_ncdb_back_useage > 80:
            nc_storage_volume_ncdb_back_useage = f'<span style="color:red;">{nc_storage_volume_ncdb_back_useage}</span>'
        else:
            nc_storage_volume_ncdb_back_useage = f'<span style="color:green;">{nc_storage_volume_ncdb_back_useage}</span>'


        nc_storage_volume_ncdb_ocr_size = round(nc_storage_volume_url_response_json_records[4]['space']["size"] / 1024 / 1024 / 1024, 2)
        nc_storage_volume_ncdb_ocr_used = round(nc_storage_volume_url_response_json_records[4]['space']["used"] / 1024 / 1024 / 1024, 2)
        nc_storage_volume_ncdb_ocr_useage = round(nc_storage_volume_url_response_json_records[4]['space']["used"] * 100 / nc_storage_volume_url_response_json_records[4]['space']["size"], 2)
        if nc_storage_volume_ncdb_ocr_useage > 80:
            nc_storage_volume_ncdb_ocr_useage = f'<span style="color:red;">{nc_storage_volume_ncdb_ocr_useage}</span>'
        else:
            nc_storage_volume_ncdb_ocr_useage = f'<span style="color:green;">{nc_storage_volume_ncdb_ocr_useage}</span>'


        nc_storage_volume_ncdg_size = round(nc_storage_volume_url_response_json_records[5]['space']["size"] / 1024 / 1024 / 1024 / 1024, 2)
        nc_storage_volume_ncdg_used = round(nc_storage_volume_url_response_json_records[5]['space']["used"] / 1024 / 1024 / 1024 / 1024, 2)
        nc_storage_volume_ncdg_useage = round(nc_storage_volume_url_response_json_records[5]['space']["used"] * 100 / nc_storage_volume_url_response_json_records[5]['space']["size"], 2)
        if nc_storage_volume_ncdg_useage > 80:
            nc_storage_volume_ncdg_useage = f'<span style="color:red;">{nc_storage_volume_ncdg_useage}</span>'
        else:
            nc_storage_volume_ncdg_useage = f'<span style="color:green;">{nc_storage_volume_ncdg_useage}</span>'


        nc_storage_volume_svm0_root_size = round(nc_storage_volume_url_response_json_records[6]['space']["size"] / 1024 / 1024 / 1024, 2)
        nc_storage_volume_svm0_root_used = round(nc_storage_volume_url_response_json_records[6]['space']["used"] / 1024 / 1024 / 1024, 2)
        nc_storage_volume_svm0_root_useage = round(nc_storage_volume_url_response_json_records[6]['space']["used"] * 100 / nc_storage_volume_url_response_json_records[6]['space']["size"], 2)
        if nc_storage_volume_svm0_root_useage > 80:
            nc_storage_volume_svm0_root_useage = f'<span style="color:red;">{nc_storage_volume_svm0_root_useage}</span>'
        else:
            nc_storage_volume_svm0_root_useage = f'<span style="color:green;">{nc_storage_volume_svm0_root_useage}</span>'


        nc_storage_volume_svm_fc_root_size = round(nc_storage_volume_url_response_json_records[7]['space']["size"] / 1024 / 1024 / 1024, 2)
        nc_storage_volume_svm_fc_root_used = round(nc_storage_volume_url_response_json_records[7]['space']["used"] / 1024 / 1024 / 1024, 2)
        nc_storage_volume_svm_fc_root_useage = round(nc_storage_volume_url_response_json_records[7]['space']["used"] * 100 / nc_storage_volume_url_response_json_records[7]['space']["size"], 2)
        if nc_storage_volume_svm_fc_root_useage > 80:
            nc_storage_volume_svm_fc_root_useage = f'<span style="color:red;">{nc_storage_volume_svm_fc_root_useage}</span>'
        else:
            nc_storage_volume_svm_fc_root_useage = f'<span style="color:green;">{nc_storage_volume_svm_fc_root_useage}</span>'


        nc_storage_volume_svm_nfs_root_size = round(nc_storage_volume_url_response_json_records[8]['space']["size"] / 1024 / 1024 / 1024, 2)
        nc_storage_volume_svm_nfs_root_used = round(nc_storage_volume_url_response_json_records[8]['space']["used"] / 1024 / 1024 / 1024, 2)
        nc_storage_volume_svm_nfs_root_useage = round(nc_storage_volume_url_response_json_records[8]['space']["used"] * 100 / nc_storage_volume_url_response_json_records[8]['space']["size"], 2)
        if nc_storage_volume_svm_nfs_root_useage > 80:
            nc_storage_volume_svm_nfs_root_useage = f'<span style="color:red;">{nc_storage_volume_svm_nfs_root_useage}</span>'
        else:
            nc_storage_volume_svm_nfs_root_useage = f'<span style="color:green;">{nc_storage_volume_svm_nfs_root_useage}</span>'


        print(nc_storage_volume_nas_muyuan_size)
        print(nc_storage_volume_nas_muyuan_used)
        print(nc_storage_volume_nas_muyuan_useage)
    else:
        print("NC存储登录失败")


    #获取NC存储日志
    nc_log_url = "https://x.x.x.x/api/support/ems/events?return_timeout=120&fields=time,node,message,log_message,index&order_by=time desc&message.severity=emergency|alert|error&time=>=24h"
    requests.packages.urllib3.disable_warnings()
    nc_log_url_response = requests.get(nc_log_url, headers=nc_headers, verify=False)
    if nc_log_url_response.status_code == 200:
        print("Login success")
        nc_log_url_response_json = nc_log_url_response.json()
        if len(nc_log_url_response_json) > 0:
            nc_log_records = nc_log_url_response_json["records"]
            nc_log_message_shuzu = []
            nc_log_message = ""
            filtered_records = [record for record in nc_log_records if record["log_message"].find("dns.server.timed.out") == -1]
            if len(filtered_records) > 0:
                for nc_records in filtered_records:
                    nc_log_message_time = nc_records["time"]
                    nc_message_severity = nc_records["message"]["severity"]
                    nc_log_message_i = nc_records["log_message"]
                    nc_log_message_shuzu.append(f"告警时间：{nc_log_message_time}</br> 日志级别：{nc_message_severity}, </br> 日志内容:{nc_log_message_i}")
                for i in nc_log_message_shuzu:
                    nc_log_message += str(i) + "\n"
            else:
                nc_log_message = "NC存储只有dns.timeout日志产生，请及时关注"

        else:
            nc_log_message = "NC存储设备无告警日志产生"

        print(nc_log_message)

    #NC存储设备总容量
    nc_storage_url = "https://x.x.x.x/api/storage/cluster"
    requests.packages.urllib3.disable_warnings()
    nc_storage_response = requests.get(nc_storage_url, headers=nc_headers, verify=False)
    if nc_storage_response.status_code == 200:
        print("Login success")
        nc_storage_response_json = nc_storage_response.json()
        nc_storage_full_size_B = nc_storage_response_json["block_storage"]["size"]
        nc_storage_full_size_T = round(nc_storage_full_size_B / 1024 / 1024 / 1024 / 1024, 2)
        print(f"存储总空间: {nc_storage_full_size_T}")
        nc_storage_full_used_B = nc_storage_response_json["block_storage"]['used']
        nc_storage_full_used_T = round(nc_storage_full_used_B / 1024 / 1024 / 1024 / 1024, 2)
        print(nc_storage_full_used_T)
        nc_storage_usage  =  round(float(nc_storage_full_used_T / nc_storage_full_size_T * 100),3)
        if nc_storage_usage > 80:
            nc_storage_usage = f'<span style="color:red;">{nc_storage_usage}</span>'
        else:
            nc_storage_usage = f'<span style="color:green;">{nc_storage_usage}</span>'


        print(nc_storage_usage)
    return {
        # -------------联想存储--财务共享--------------------------
        'fssc_log_message': fssc_log_message,
        'fssc_storage_full_size_T': fssc_storage_full_size_T,
        'fssc_storage_full_used_T': fssc_storage_full_used_T,
        'fssc_storage_usage': fssc_storage_usage,
        'storage_vloumes_json_size_T': storage_vloumes_json_size_T,
        'storage_vloumes_json_used_T': storage_vloumes_json_used_T,
        'caiwunas_usage': caiwunas_usage,
        # ----------联想存储--NC---------------------
        'nc_log_message': nc_log_message,
        'nc_storage_full_size_T': nc_storage_full_size_T,
        'nc_storage_full_used_T': nc_storage_full_used_T,
        'nc_storage_usage': nc_storage_usage,
        'nc_storage_volume_nas_muyuan_size': nc_storage_volume_nas_muyuan_size,
        'nc_storage_volume_nas_muyuan_used': nc_storage_volume_nas_muyuan_used,
        'nc_storage_volume_nas_muyuan_useage': nc_storage_volume_nas_muyuan_useage,
        'nc_storage_volume_ncdb_asm_size': nc_storage_volume_ncdb_asm_size,
        'nc_storage_volume_ncdb_asm_used': nc_storage_volume_ncdb_asm_used,
        'nc_storage_volume_ncdb_asm_useage': nc_storage_volume_ncdb_asm_useage,
        'nc_storage_volume_ncdb_asm2_size': nc_storage_volume_ncdb_asm2_size,
        'nc_storage_volume_ncdb_asm2_used': nc_storage_volume_ncdb_asm2_used,
        'nc_storage_volume_ncdb_asm2_useage': nc_storage_volume_ncdb_asm2_useage,
        'nc_storage_volume_ncdb_back_size': nc_storage_volume_ncdb_back_size,
        'nc_storage_volume_ncdb_back_used': nc_storage_volume_ncdb_back_used,
        'nc_storage_volume_ncdb_back_useage': nc_storage_volume_ncdb_back_useage,
        'nc_storage_volume_ncdb_ocr_size': nc_storage_volume_ncdb_ocr_size,
        'nc_storage_volume_ncdb_ocr_used': nc_storage_volume_ncdb_ocr_used,
        'nc_storage_volume_ncdb_ocr_useage': nc_storage_volume_ncdb_ocr_useage,
        'nc_storage_volume_ncdg_size': nc_storage_volume_ncdg_size,
        'nc_storage_volume_ncdg_used': nc_storage_volume_ncdg_used,
        'nc_storage_volume_ncdg_useage': nc_storage_volume_ncdg_useage,
        'nc_storage_volume_svm0_root_size': nc_storage_volume_svm0_root_size,
        'nc_storage_volume_svm0_root_used': nc_storage_volume_svm0_root_used,
        'nc_storage_volume_svm0_root_useage': nc_storage_volume_svm0_root_useage,
        'nc_storage_volume_svm_fc_root_size': nc_storage_volume_svm_fc_root_size,
        'nc_storage_volume_svm_fc_root_used': nc_storage_volume_svm_fc_root_used,
        'nc_storage_volume_svm_fc_root_useage': nc_storage_volume_svm_fc_root_useage,
        'nc_storage_volume_svm_nfs_root_size': nc_storage_volume_svm_nfs_root_size,
        'nc_storage_volume_svm_nfs_root_used': nc_storage_volume_svm_nfs_root_used,
        'nc_storage_volume_svm_nfs_root_useage': nc_storage_volume_svm_nfs_root_useage,
    }

lenovo_cunchu()